home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 44
/
Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso
/
Aminet
/
dev
/
moni
/
systemviewer.lha
/
SysSemaphore.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-04-25
|
16KB
|
652 lines
/****h* SysSemaphore.c [1.1] ******************************************
*
* NAME
* SysSemaphore.c
*
* DESCRIPTION
* Display a list of all Semaphores currently known to the OS.
*
* FUNCTIONAL INTERFACE:
*
*
*
* FUNCTIONS FROM SysCommon.c:
*
* PUBLIC BOOL SanityCheck( char *msg, ULONG addr );
*
* PUBLIC int SetupSystemList( (*OpenWindowFunc)( void ) );
*
* PUBLIC void ShutdownSystemList( void );
*
* PUBLIC void ModifyListView( struct Gadget *lv,
* struct Window *w,
* struct List *list,
* struct Gadget *strgadget
* );
*
* GUI Designed by : Jim Steichen
*
***********************************************************************
*
*/
#include <string.h>
#include <exec/types.h>
#include <exec/execbase.h>
#include <AmigaDOSErrs.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/diskfont_protos.h>
#include "CPGM:GlobalObjects/CommonFuncs.h"
#include "SysLists.h"
#define SemLV 0
#define Update 1
#define Remove 2
#define Cancel 3
#define Release 4
#define SemDisplay 5
#define SEM_CNT 6
#define LISTVIEWGADGET SemGadgets[ SemLV ]
#define SEM_DISPLAY SemGadgets[ SemDisplay ]
PUBLIC struct List *SListPtr = NULL;
PRIVATE char ver[] = "\0$VER: SysSemaphore 1.1 (25-Apr-2001) by J.T. Steichen\0";
PRIVATE struct Window *SemWnd = NULL;
PRIVATE struct Gadget *SemGList = NULL;
PRIVATE struct IntuiMessage SemMsg;
PRIVATE struct Gadget *SemGadgets[ SEM_CNT ];
PRIVATE UWORD SemLeft = 0;
PRIVATE UWORD SemTop = 16;
PRIVATE UWORD SemWidth = 632;
PRIVATE UWORD SemHeight = 250;
PRIVATE UBYTE *SemWdt = (UBYTE *) "System Semaphores Info:";
PUBLIC struct TextFont *SemFont = NULL;
// -------------------------------------------------------------------
PRIVATE struct IntuiText SemIText[] = {
2, 0, JAM1, 0, 7, NULL,
"Address: Pri: Nest Queue Count Name Owner Task",
NULL
};
#define MAXSEMS 30
#define NODELENGTH 80
PRIVATE struct List SempList;
PRIVATE struct Node SempNodes[ MAXSEMS ] = { NULL, };
PRIVATE UBYTE NodeStrs[ MAXSEMS * NODELENGTH ] = "";
PRIVATE struct ListViewMem lvm = { 0, };
// -------------------------------------------------------------------
PRIVATE UWORD SemGTypes[] = {
LISTVIEW_KIND, BUTTON_KIND, BUTTON_KIND,
BUTTON_KIND, BUTTON_KIND, TEXT_KIND
};
PRIVATE int SemLVClicked( int whichitem );
PRIVATE int UpdateClicked( int dummy );
PRIVATE int RemoveClicked( int dummy );
PRIVATE int CancelClicked( int dummy );
PRIVATE int ReleaseClicked( int dummy );
PRIVATE struct NewGadget SemNGad[] = {
2, 14, 627, 184, NULL, NULL, SemLV,
0, NULL, (APTR) SemLVClicked,
2, 226, 71, 17, (UBYTE *) "_Update", NULL, Update,
PLACETEXT_IN, NULL, (APTR) UpdateClicked,
396, 226, 71, 17, (UBYTE *) "Remove", NULL, Remove,
PLACETEXT_IN, NULL, (APTR) RemoveClicked,
554, 226, 71, 17, (UBYTE *) "_Cancel", NULL, Cancel,
PLACETEXT_IN, NULL, (APTR) CancelClicked,
82, 226, 71, 17, (UBYTE *) "Release", NULL, Release,
PLACETEXT_IN, NULL, (APTR) ReleaseClicked,
2, 202, 611, 16, NULL, NULL, SemDisplay,
0, NULL, NULL
};
PRIVATE ULONG SemGTags[] = {
GTLV_ShowSelected, NULL,
LAYOUTA_Spacing, 2, TAG_DONE,
GT_Underscore, '_', TAG_DONE,
GA_Disabled, TRUE, TAG_DONE,
GT_Underscore, '_', TAG_DONE,
GA_Disabled, TRUE, TAG_DONE,
GTTX_Border, TRUE, TAG_DONE
};
// -------------------------------------------------------------------
PRIVATE struct SignalSemaphore *CurrentSem = NULL;
PRIVATE int InitializeSemaphoreList( void )
{
IMPORT struct ExecBase *SysBase;
struct Task *tc = NULL;
char *nm = NULL;
int i = 0;
HideListFromView( LISTVIEWGADGET, SemWnd );
Forbid();
SListPtr = &SysBase->SemaphoreList;
CurrentSem = (struct SignalSemaphore *) SListPtr->lh_Head;
while (i < MAXSEMS && CurrentSem != NULL)
{
tc = CurrentSem->ss_Owner;
nm = CurrentSem->ss_Link.ln_Name;
sprintf( &NodeStrs[ i++ * NODELENGTH ],
"%08LX %4d %4u %08LX %08LX %-20.20s %-30.30s",
CurrentSem,
CurrentSem->ss_Link.ln_Pri,
CurrentSem->ss_NestCount,
&CurrentSem->ss_WaitQueue,
CurrentSem->ss_QueueCount,
nm == NULL ? "** NO NAME **" : nm,
tc == NULL ? "** UNKNOWN **" : tc->tc_Node.ln_Name
);
CurrentSem = (struct SignalSemaphore *)
CurrentSem->ss_Link.ln_Succ;
}
// Reset CurrentSem to the Head of the list.
CurrentSem = (struct SignalSemaphore *) SListPtr->lh_Head;
Permit();
GT_SetGadgetAttrs( LISTVIEWGADGET, SemWnd, NULL,
GTLV_Labels, &SempList,
GTLV_ShowSelected, SEM_DISPLAY,
GTLV_Selected, 0,
GTLV_ItemHeight, 12,
TAG_END
);
DisplayTitle( SemWnd, SemWdt );
return( i );
}
PRIVATE char wt[80] = { 0, }, *modtitle = &wt[0];
PRIVATE int SemLVClicked( int whichitem )
{
ULONG addr = 0L;
// Get address from the item:
(void) stch_l( SempNodes[ whichitem ].ln_Name, (long *) &addr );
CurrentSem = addr;
if (addr != NULL)
{
sprintf( modtitle, "%-45.45s You Selected: %08LX", SemWdt, addr );
DisplayTitle( SemWnd, modtitle );
GT_SetGadgetAttrs( SEM_DISPLAY, SemWnd, NULL,
GTTX_Text,
(STRPTR) &NodeStrs[ NODELENGTH * whichitem ],
TAG_DONE
);
}
// Enable buttons because user selected a Semaphore from the ListView:
GT_SetGadgetAttrs( SemGadgets[ Remove ], SemWnd, NULL,
GA_Disabled, FALSE, TAG_DONE
);
GT_SetGadgetAttrs( SemGadgets[ Release ], SemWnd, NULL,
GA_Disabled, FALSE, TAG_DONE
);
return( (int) TRUE );
}
PRIVATE int UpdateClicked( int dummy )
{
int i;
for (i = 0; i < MAXSEMS; i++) // 0 = No ListView title.
NodeStrs[ i * NODELENGTH ] = '\0'; // Kill old ListView strings.
if (InitializeSemaphoreList() <= 0)
{
// No Semaphores known to SysBase!!
UserInfo( "No Semaphores found!", "OS ERROR??" );
}
GT_RefreshWindow( SemWnd, NULL );
return( (int) TRUE );
}
PRIVATE void RemoveTheSemaphore( struct SignalSemaphore *ss )
{
// We should probably check that the ss->ss_QueueCount is 0 first:
// ObtainSemaphoreList( SysBase->SemaphoreList );
// Forbid();
// RemSemaphore( ss );
// ObtainSemaphore( ss );
// ReleaseSemaphore( ss );
// Permit();
// ReleaseSemaphoreList( SysBase->SemaphoreList );
UserInfo( "NOT Implemented yet!", "User Information:" );
return;
}
PRIVATE int RemoveClicked( int dummy )
{
/* Do a SanityCheck() before Removing the Semaphore: */
// Check CurrentSem for a non-NULL value
if (CurrentSem != NULL)
{
if (SanityCheck( "Are you SURE you want to Remove %08LX?" ) != FALSE)
{
RemoveTheSemaphore( CurrentSem );
if (InitializeSemaphoreList() <= 0)
{
// No Semaphores known to SysBase!!
UserInfo( "No Semaphores found!", "OS ERROR??" );
}
GT_RefreshWindow( SemWnd, NULL );
}
}
else
{
// No Semaphores selected by the user:
UserInfo( "Select a Semaphores first!", "USER ERROR:" );
return( (int) TRUE );
}
return( (int) TRUE );
}
PRIVATE void ReleaseTheSemaphore( struct SignalSemaphore *ss )
{
// int i;
// We should probably check that the ss->ss_QueueCount is 0 first:
// ObtainSemaphoreList( SysBase->SemaphoreList );
// for (i = 0; i < ss->ss_NestCount; i++)
// ReleaseSemaphore( ss );
// ReleaseSemaphoreList( SysBase->SemaphoreList );
UserInfo( "NOT Implemented yet!", "User Information:" );
return;
}
PRIVATE int ReleaseClicked( int dummy )
{
char t[80];
/* Do a SanityCheck() before Releasing the Semaphore: */
// Check CurrentSem for a non-NULL value
if (CurrentSem != NULL)
{
sprintf( &t[0], "Are you SURE you want to Release %08LX?",
CurrentSem
);
if (SanityCheck( &t[0] )!= FALSE)
{
ReleaseTheSemaphore( CurrentSem );
if (InitializeSemaphoreList() <= 0)
{
// No Semaphores known to SysBase!!
UserInfo( "No Semaphores found!", "OS ERROR??" );
}
GT_RefreshWindow( SemWnd, NULL );
}
else
return( (int) TRUE );
}
else
{
// No Semaphores selected by the user:
UserInfo( "Select a Semaphores first!", "USER ERROR:" );
return( (int) TRUE );
}
return( (int) TRUE );
}
PRIVATE void CloseSemWindow( void )
{
if (SemWnd != NULL)
{
CloseWindow( SemWnd );
SemWnd = NULL;
}
if (SemGList != NULL)
{
FreeGadgets( SemGList );
SemGList = NULL;
}
if (SemFont != NULL)
{
CloseFont( SemFont );
SemFont = NULL;
}
return;
}
PRIVATE int SemCloseWindow( void )
{
CloseSemWindow();
return( (int) FALSE );
}
PRIVATE int CancelClicked( int dummy )
{
return( SemCloseWindow() );
}
PRIVATE void SemRender( void )
{
struct IntuiText it;
ComputeFont( Scr, Font, &CFont, SemWidth, SemHeight );
CopyMem( (char *) &SemIText, (char *) &it,
(long) sizeof( struct IntuiText )
);
it.ITextFont = Font;
it.LeftEdge = 10;
/*
it.LeftEdge = CFont.OffX + ComputeX( CFont.FontX, it.LeftEdge )
- (IntuiTextLength( &it ) >> 1);
*/
it.TopEdge = CFont.OffY + ComputeY( CFont.FontY, it.TopEdge )
- (Font->ta_YSize >> 1);
PrintIText( SemWnd->RPort, &it, 0, 0 );
return;
}
PRIVATE int OpenSemWindow( void )
{
struct NewGadget ng;
struct Gadget *g;
UWORD lc, tc;
UWORD wleft = SemLeft, wtop = SemTop, ww, wh;
ComputeFont( Scr, Font, &CFont, SemWidth, SemHeight );
ww = ComputeX( CFont.FontX, SemWidth );
wh = ComputeY( CFont.FontY, SemHeight );
if ((wleft + ww + CFont.OffX + Scr->WBorRight) > Scr->Width)
wleft = Scr->Width - ww;
if ((wtop + wh + CFont.OffY + Scr->WBorBottom) > Scr->Height)
wtop = Scr->Height - wh;
if ((SemFont = OpenDiskFont( Font )) == NULL)
return( -5 );
if ((g = CreateContext( &SemGList )) == NULL)
return( -1 );
for (lc = 0, tc = 0; lc < SEM_CNT; lc++)
{
CopyMem( (char *) &SemNGad[lc], (char *) &ng,
(long) sizeof( struct NewGadget )
);
ng.ng_VisualInfo = VisualInfo;
ng.ng_TextAttr = Font;
ng.ng_LeftEdge = CFont.OffX + ComputeX( CFont.FontX,
ng.ng_LeftEdge
);
ng.ng_TopEdge = CFont.OffY + ComputeY( CFont.FontY,
ng.ng_TopEdge
);
ng.ng_Width = ComputeX( CFont.FontX, ng.ng_Width );
ng.ng_Height = ComputeY( CFont.FontY, ng.ng_Height );
SemGadgets[lc] = g
= CreateGadgetA( (ULONG) SemGTypes[lc],
g,
&ng,
(struct TagItem *) & SemGTags[tc]
);
while (SemGTags[tc] != TAG_DONE)
tc += 2;
tc++;
if (g == NULL)
return( -2 );
}
if ((SemWnd = OpenWindowTags( NULL,
WA_Left, wleft,
WA_Top, wtop,
WA_Width, ww + CFont.OffX + Scr->WBorRight,
WA_Height, wh + CFont.OffY + Scr->WBorBottom,
WA_IDCMP, LISTVIEWIDCMP | BUTTONIDCMP
| TEXTIDCMP | IDCMP_CLOSEWINDOW
| IDCMP_REFRESHWINDOW | IDCMP_VANILLAKEY,
WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET
| WFLG_CLOSEGADGET | WFLG_SMART_REFRESH
| WFLG_ACTIVATE | WFLG_RMBTRAP,
WA_Gadgets, SemGList,
WA_Title, SemWdt,
WA_ScreenTitle, "System Info",
TAG_DONE )
) == NULL)
return( -4 );
GT_RefreshWindow( SemWnd, NULL );
SemRender();
return( 0 );
}
PRIVATE int SemVanillaKey( int whichkey )
{
int rval = TRUE;
switch (whichkey)
{
case 'u':
case 'U':
UpdateClicked( 0 );
break;
case 'c':
case 'C':
case 'q':
case 'Q':
rval = SemCloseWindow();
break;
}
return( rval );
}
PRIVATE int HandleSemIDCMP( void )
{
struct IntuiMessage *m;
int (*func)( int code );
BOOL running = TRUE;
while (running == TRUE)
{
if ((m = GT_GetIMsg( SemWnd->UserPort ) ) == NULL )
{
(void) Wait( 1L << SemWnd->UserPort->mp_SigBit );
continue;
}
CopyMem( (char *) m, (char *) &SemMsg,
(long) sizeof( struct IntuiMessage )
);
GT_ReplyIMsg( m );
switch (SemMsg.Class)
{
case IDCMP_REFRESHWINDOW:
GT_BeginRefresh( SemWnd );
SemRender();
GT_EndRefresh( SemWnd, TRUE );
break;
case IDCMP_CLOSEWINDOW:
running = SemCloseWindow();
break;
case IDCMP_VANILLAKEY:
running = SemVanillaKey( SemMsg.Code );
break;
case IDCMP_GADGETUP:
case IDCMP_GADGETDOWN:
func = (void *) ((struct Gadget *) SemMsg.IAddress)->UserData;
if (func != NULL)
running = func( (int) SemMsg.Code );
break;
}
}
return( running );
}
PUBLIC int HandleSemaphoreLV( void )
{
int i = 0;
if (SetupSystemList( &OpenSemWindow ) < 0)
{
(void) Handle_Problem( "Couldn't open a System ListViewer!",
"Allocation Problem:", NULL
);
return( -1 );
}
// Disable buttons until user selects a Semaphore from the ListView:
GT_SetGadgetAttrs( SemGadgets[ Remove ], SemWnd, NULL,
GA_Disabled, TRUE, TAG_DONE
);
GT_SetGadgetAttrs( SemGadgets[ Release ], SemWnd, NULL,
GA_Disabled, TRUE, TAG_DONE
);
SetNotifyWindow( SemWnd ); // For Handle_Problem().
lvm.lvm_NodeStrs = &NodeStrs[0];
lvm.lvm_Nodes = &SempNodes[0];
lvm.lvm_NumItems = MAXSEMS;
lvm.lvm_NodeLength = NODELENGTH;
SetupList( &SempList, &lvm );
(void) InitializeSemaphoreList();
GT_SetGadgetAttrs( LISTVIEWGADGET, SemWnd, NULL,
GTLV_Labels, &SempList,
GTLV_ShowSelected, SEM_DISPLAY,
GTLV_Selected, 0,
GTLV_ItemHeight, 12,
TAG_END
);
GT_RefreshWindow( SemWnd, NULL );
(void) HandleSemIDCMP();
ShutdownSystemList();
return( 0 );
}
#ifdef DEBUG
PUBLIC int main( void )
{
return( HandleSemaphoreLV() );
}
#endif
/* --------------------- END of SysSemaphore.c file! --------------- */